define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    //注入自定义js-切片上传文件到华为OBS
    function ZhuRuJS(url){
        let temp=document.createElement('script');
        temp.setAttribute('type','text/javascript');
        temp.src=url;
        document.head.appendChild(temp);
    }
    ZhuRuJS(location.origin+'/assets/js/common/slicesUpload.js');

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'cms/introduce/index' + location.search,
                    add_url: 'cms/introduce/add',
                    edit_url: 'cms/introduce/edit',
                    del_url: 'cms/introduce/del',
                    multi_url: 'cms/introduce/multi',
                    import_url: 'cms/introduce/import',
                    table: 'introduce',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                commonSearch: false,//取消搜索按钮
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate: false},
                        {field: 'channels_name', title: __('Channels_id'), operate: false},
                        {field: 'admin.nickname', title: __('Admin.nickname'), operate: false},
                        {field: 'content', title: __('Content'), operate: false,search: true},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'cms/introduce/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'cms/introduce/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'cms/introduce/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };

    /*
    //切片上传
    var sectionUpload = $('.section_upload');
    if(sectionUpload){
        // 点击上传按钮触发文件选择并自动上传
        $('#startUploadButton').on('click', function () {
            $('#fileInput').click();
        });

        // 当选择文件后自动开始上传
        $('#fileInput').on('change', function() {
            // 阻止表单的默认提交行为
            event.preventDefault();

            // 获取选定的文件
            let file = $('#fileInput')[0].files[0];
            // 获取文件名
            let file_name = file.name;
            let partSize = 1 * 1024 * 1024; // 分段大小，这里设置为5MB

            //获取分片上传初始化信息
            // 调用函数并处理返回结果
            var init_data = getUploadConfig(file_name);
            if (!init_data){
                alert('初始化错误')
                return false
            }
            var path =init_data.path//文件合并后在obs上的保存路径
            var fileId =init_data.upload_id//切片上传的

            // 数据切片
            var chunks = fileSlice(file,partSize);

            // 发送分割数据段
            uploadPart(file_name,fileId, chunks, path,0,[]);

        });

        //获取分段上传配置信息
        function getUploadConfig(file_name){
            var res_data;
            $.ajax({
                url : 'upload_file/initiateMultipartUpload',
                type : "post",
                data : {
                    'file_name':file_name
                },
                async : false,
                success : function(res) {
                    console.log(res,res.data)
                    if (res.code){
                        res_data = res.data
                    }
                }
            });
            return res_data;
        }

        //数据切片
        function fileSlice(file, chunkSize = 1024*1024*0.2){
            // 1.初始化数据
            var totalSize = file.size;
            var start = 0;
            var end = start + chunkSize;
            var chunks = [];
            // 2.使用bolb提供的slice方法切片
            while(start < totalSize){
                var chunk = file.slice(start, end);
                chunks.push(chunk);
                start = end;
                end += chunkSize;
            }
            // 3.返回切片组chunk[]
            return chunks;
        }

        //执行逐个上传
        function uploadPart(file_name,upload_id, chunks, path,index = 0,parts = []) {
            if (index >= chunks.length) {
                // 所有部分上传完成
                makeFileIntegration(file_name,upload_id, chunks.length, parts, path);
                return;
            }

            var chunk = chunks[index];
            var formData = new FormData();
            formData.append('upload_id', upload_id);
            formData.append('chunk', chunk);
            formData.append('path', path);

            let part_number = index + 1
            formData.append('part_number', part_number);

            $.ajax({
                type: "POST",
                url: 'upload_file/uploadPart',
                data: formData,
                contentType: false,
                processData: false,
                success: function (done) {
                    if (done.code) {
                        parts.push(done.data);
                        console.log(done, ' 已完成');
                        uploadPart(file_name,upload_id, chunks, path,part_number,parts); // 递归调用上传下一个部分
                    }
                }
            });
        }

        //通知整合方法
        function makeFileIntegration(file_name,upload_id, size,parts,path){
            // 通知已传输完成
            $.ajax({
                url : 'upload_file/completeMultipartUpload',
                type : "post",
                data : {
                    'file_name':file_name,
                    'upload_id':upload_id,
                    'parts':parts,
                    'path':path,
                },
                async : false,
                success : function(res) {
                    console.log(res)
                    if (res.code){
                        console.log(res.data)

                        let val_data = $('#c-file_data').val();
                        res.data.filename
                        res.data.fullurl
                        res.data.url


                        alert('上传成功')
                    }
                }
            });
        }

        function getInputData(id_name){

        }
    }
     */

    return Controller;
});
